import logging

import pytest

from resource.pom.gallery import Gallery
from resource.pom.launcher import Launcher
from resource.pom.memorandum import Memorandum


class Test:
    app = Memorandum

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('预置条件.清除测试数据')
        device.hdc_shell('rm -rf /data/app/el2/100/database/com.ohos.note/default/rdb/note.db')
        device.wait(2)
        logging.info('步骤1.打开备忘录标签页，有结果1')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('数学公式'))
        logging.info('预期结果1.打开正常')
        device.refresh_layout()
        device.assert_text_exist('数学公式')
        device.assert_text_exist('好好学习，天天向上')

        logging.info('步骤2.编辑备忘录内容，有结果2')
        device.click(*self.app.content_area)
        logging.info('预期结果2.弹出键盘，可以编辑内容')
        device.wait(2)
        device.assert_soft_keyboard_on()

        logging.info('步骤3.备忘录返回，有结果3')
        device.click(*self.app.back)
        logging.info('预期结果3.键盘收起，正常返回')
        device.refresh_layout()
        device.assert_soft_keyboard_on(expect=False)
        device.assert_text_exist('全部笔记')

        logging.info('步骤4.返回之后，再次打开标签页，有结果4')
        device.refresh_layout()
        device.click_element(device.get_element_by_text('数学公式'))
        logging.info('预期结果4.正常打开')
        device.refresh_layout()
        device.assert_text_exist('数学公式')
        device.assert_text_exist('好好学习，天天向上')

        logging.info('步骤5.点击recent按钮，然后返回备忘录，有结果5')
        device.click(*self.app.content_area)
        device.wait(2)
        device.press_recent_key()
        logging.info('预期结果5.点击recent后键盘收起，返回备忘录后键盘恢复')
        device.assert_current_window(Launcher.recent_view)
        device.assert_soft_keyboard_on(expect=False)
        device.click(*self.app.content_area)
        device.assert_soft_keyboard_on()

        logging.info('步骤6.备忘录点击新建笔记，清单、样式、相册按钮，有结果6')
        device.click(*self.app.back)
        device.click(*self.app.add_note)
        device.wait(1)
        device.click(*self.app.content_area)
        device.wait(2)
        device.refresh_layout()
        # 清单、样式、相册
        todo1 = device.get_element_by_text('todo1')
        styles = device.get_element_by_text('styles')
        picture_white = device.get_element_by_text('picture_white')
        logging.info('预期结果6.1 点击清单跳转至对应按钮界面')
        device.click_element(todo1)
        device.refresh_layout()
        device.assert_type_exist('checkBox')

        logging.info('预期结果6.2 点击样式弹出样式对话框')
        device.click(*self.app.content_area)
        device.click_element(styles)
        device.refresh_layout()
        device.assert_type_exist('Dialog')
        device.assert_type_exist('Button')
        device.assert_type_exist('Circle')
        device.assert_type_exist('Slider')

        logging.info('预期结果6.3 点击相册')
        device.click(*self.app.close_style)
        device.click(*self.app.content_area)
        device.click_element(picture_white)
        device.refresh_layout()
        allow = device.get_element_by_text('允许')
        if allow:
            device.click_element(allow)
        device.wait(2)
        device.assert_current_window(Gallery.window_name)
        device.refresh_layout()
        device.assert_text_exist('所有照片')
